package com.colldown.iservice.sysconfig.web.impl;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.colldown.common.A_Tools;
import com.colldown.common.Pager;
import com.colldown.iservice.sysconfig.service.ISysConfigService;
import com.colldown.iservice.sysconfig.web.ISysConfigGrid;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
@Controller
@RequestMapping("/configGrid")
public class SysConfigGridImpl implements ISysConfigGrid {
	@Autowired
	ISysConfigService sysConfigService;

	@Override
	@RequestMapping("/queryDeviceList")
	public String queryDeviceList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		//是否为ajax取数据标志
		String ajaxFlag = A_Tools.NullToStr(request.getParameter("ajaxFlag"));
		
		if("1".equals(ajaxFlag)){
			String Station_ID = A_Tools.NullToStr(request.getParameter("STATION_ID"));
			String Station_Name = A_Tools.NullToStr(request.getParameter("STATION_NAME"));
			try {
				Station_Name = URLDecoder.decode(Station_Name,"UTF-8");
				Station_ID = URLDecoder.decode(Station_ID,"UTF-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
			String Store_Mode = A_Tools.NullToStr(request.getParameter("STORE_MODE"));
			String Time_Source = A_Tools.NullToStr(request.getParameter("TIME_SOURCE"));
			//查询参数map
			Map paramMap = new HashMap();
			paramMap.put("STATION_ID",Station_ID );
			paramMap.put("STATION_NAME",Station_Name );
			paramMap.put("STORE_MODE",Store_Mode );
			paramMap.put("TIME_SOURCE",Time_Source );
			int iDisplayStart =A_Tools.NullToNumber(request.getParameter("iDisplayStart"));
			int iDisplayLength = A_Tools.NullToNumber(request.getParameter("iDisplayLength"));
			String sEcho = A_Tools.NullToStr(request.getParameter("sEcho"));
			List list = new ArrayList();
			PageHelper.startPage(iDisplayStart, iDisplayLength);
			list = this.sysConfigService.queryDeviceList(paramMap);
			//将Station_Name由byte[]转化为string
			for(Map m:(List<Map>)list){
				m.put("STATION_NAME",new String((byte[])m.get("STATION_NAME")).trim());
			}
			PageInfo pg = new PageInfo(list);
			
			/*
	        for(int i=0;i<5;i++){
	        	Map map = new HashMap();
	        	map.put("name", "Tiger Nixon"+i);
	        	map.put("position", "System Architect"+i);
	        	map.put("salary", "$320,800");
	        	map.put("start_date", "2011-04-25");
	        	map.put("office", "Edinburgh");
	        	map.put("extn", "Edinburgh");
	        	list.add(map);
	        }
	        */
	        Pager page = new Pager();
	        page.setDataResult((List<Map>)list);
	        page.setiTotalRecords(pg.getTotal());
	        page.setiTotalDisplayRecords(pg.getTotal());
	        page.setiDisplayLength(iDisplayLength);
	        page.setsEcho(sEcho);
	        Gson gson = new Gson();
			A_Tools.printJson(response, gson.toJson(page));
		}
		return "deviceGrid";
		
	}

	@Override
	@RequestMapping("/queryNetDeviceList")
	public String queryNetDeviceList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		//是否为ajax取数据标志
		String ajaxFlag = A_Tools.NullToStr(request.getParameter("ajaxFlag"));
		
		if("1".equals(ajaxFlag)){
			String Eth_ID = A_Tools.NullToStr(request.getParameter("ETH_ID"));
			String Eth_Name = A_Tools.NullToStr(request.getParameter("ETH_NAME"));
			try {
				Eth_Name = URLDecoder.decode(Eth_Name,"UTF-8");
				Eth_ID = URLDecoder.decode(Eth_ID,"UTF-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
			//查询参数map
			Map paramMap = new HashMap();
			paramMap.put("ETH_ID",Eth_ID );
			paramMap.put("ETH_NAME",Eth_Name );
			int iDisplayStart =A_Tools.NullToNumber(request.getParameter("iDisplayStart"));
			int iDisplayLength = A_Tools.NullToNumber(request.getParameter("iDisplayLength"));
			String sEcho = A_Tools.NullToStr(request.getParameter("sEcho"));
			List list = new ArrayList();
			PageHelper.startPage(iDisplayStart, iDisplayLength);
			list = this.sysConfigService.queryNetDeviceList(paramMap);
			//将Station_Name由byte[]转化为string
			for(Map m:(List<Map>)list){
				m.put("ETH_NAME",new String((byte[])m.get("ETH_NAME")).trim());
			}
			PageInfo pg = new PageInfo(list);
	        Pager page = new Pager();
	        page.setDataResult((List<Map>)list);
	        page.setiTotalRecords(pg.getTotal());
	        page.setiTotalDisplayRecords(pg.getTotal());
	        page.setiDisplayLength(iDisplayLength);
	        page.setsEcho(sEcho);
	        Gson gson = new Gson();
			A_Tools.printJson(response, gson.toJson(page));
		}
		return "netDeviceGrid";
		
	}

}
